/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateMountpoint
/*     */   extends MailDocumentHandler
/*     */ {
/*  44 */   private static final String[] TARGET_FOLDER_PATH = { "link", "l" };
/*  45 */   private static final String[] RESPONSE_ITEM_PATH = new String[0];
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  49 */     return TARGET_FOLDER_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  54 */     return true;
/*     */   }
/*     */   
/*     */   protected String[] getResponseItemPath()
/*     */   {
/*  59 */     return RESPONSE_ITEM_PATH;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  64 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  65 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  66 */     OperationContext octxt = getOperationContext(zsc, context);
/*  67 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  69 */     Element t = request.getElement("link");
/*  70 */     String name = t.getAttribute("name");
/*  71 */     String view = t.getAttribute("view", null);
/*  72 */     String flags = t.getAttribute("f", null);
/*  73 */     byte color = (byte)(int)t.getAttributeLong("color", 0L);
/*  74 */     String rgb = t.getAttribute("rgb", null);
/*  75 */     ItemId iidParent = new ItemId(t.getAttribute("l"), zsc);
/*  76 */     boolean fetchIfExists = t.getAttributeBool("fie", false);
/*  77 */     boolean reminderEnabled = t.getAttributeBool("reminder", false);
/*     */     
/*  79 */     Account target = null;
/*  80 */     String ownerId = t.getAttribute("zid", null);
/*  81 */     if (ownerId == null) {
/*  82 */       String ownerName = t.getAttribute("owner");
/*  83 */       target = Provisioning.getInstance().get(Key.AccountBy.name, ownerName, zsc.getAuthToken());
/*     */       
/*     */ 
/*  86 */       if (target == null) {
/*  87 */         throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
/*     */       }
/*  89 */       ownerId = target.getId();
/*  90 */       if (ownerId.equalsIgnoreCase(zsc.getRequestedAccountId())) {
/*  91 */         throw ServiceException.INVALID_REQUEST("cannot mount your own folder", null);
/*     */       }
/*     */     }
/*  94 */     Element remote = fetchRemoteFolder(zsc, context, ownerId, (int)t.getAttributeLong("rid", -1L), t.getAttribute("path", null));
/*  95 */     int remoteId = new ItemId(remote.getAttribute("id"), zsc).getId();
/*  96 */     String remoteUuid = remote.getAttribute("uuid", null);
/*  97 */     if (view == null) {
/*  98 */       view = remote.getAttribute("view", null);
/*     */     }
/*     */     try
/*     */     {
/* 102 */       Color itemColor = rgb != null ? new Color(rgb) : new Color(color);
/* 103 */       mpt = mbox.createMountpoint(octxt, iidParent.getId(), name, ownerId, remoteId, remoteUuid, MailItem.Type.of(view), Flag.toBitmask(flags), itemColor, reminderEnabled);
/*     */     } catch (ServiceException se) { Mountpoint mpt;
/*     */       Mountpoint mpt;
/* 106 */       if ((se.getCode() == "mail.ALREADY_EXISTS") && (fetchIfExists)) {
/* 107 */         Folder folder = mbox.getFolderByName(octxt, iidParent.getId(), name);
/* 108 */         if ((folder instanceof Mountpoint)) {
/* 109 */           mpt = (Mountpoint)folder;
/*     */         } else
/* 111 */           throw se;
/*     */       } else {
/* 113 */         throw se;
/*     */       }
/*     */     }
/*     */     Mountpoint mpt;
/* 117 */     Element response = zsc.createElement(MailConstants.CREATE_MOUNTPOINT_RESPONSE);
/* 118 */     if (mpt != null) {
/* 119 */       Element eMount = ToXML.encodeMountpoint(response, ifmt, octxt, mpt);
/*     */       
/* 121 */       ToXML.transferMountpointContents(eMount, remote);
/*     */     }
/* 123 */     return response;
/*     */   }
/*     */   
/*     */   private Element fetchRemoteFolder(ZimbraSoapContext zsc, Map<String, Object> context, String ownerId, int remoteId, String remotePath) throws ServiceException
/*     */   {
/* 128 */     Element request = zsc.createRequestElement(MailConstants.GET_FOLDER_REQUEST);
/* 129 */     if (remoteId > 0) {
/* 130 */       request.addElement("folder").addAttribute("l", remoteId);
/* 131 */     } else if (remotePath != null) {
/* 132 */       request.addElement("folder").addAttribute("path", remotePath);
/*     */     } else {
/* 134 */       throw ServiceException.INVALID_REQUEST("must specify one of rid/path", null);
/*     */     }
/* 136 */     Element response = proxyRequest(request, context, ownerId);
/* 137 */     Element remote = response.getOptionalElement("folder");
/* 138 */     if (remote == null)
/* 139 */       throw ServiceException.INVALID_REQUEST("cannot mount a search or mountpoint", null);
/* 140 */     return remote;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CreateMountpoint.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */